package gx.web.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import gx.entity.Manager;

import gx.entity.Category;
import gx.entity.Product;
import gx.entity.common.Page;
import gx.service.CategoryService;
import gx.service.ProductService;

@Controller
@RequestMapping("/mng/prod")
public class ProductController {
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;

@RequestMapping("/list.jhtml" )
//根据ID查询详情
public ModelAndView list(@RequestParam(name="size",defaultValue="10")int size,
		@RequestParam(name="number",defaultValue="1")int number){
	
		ModelAndView mav=new ModelAndView("/mng/prod/list");
		Page<Product> page=productService.findByPager(size, number);
		mav.addObject("page", page);
		return mav;
		
}
//打开新增页面时，预加载商品分类下拉列表
@RequestMapping("/toAdd.jhtml")
public ModelAndView toAdd(){
	ModelAndView mav=new ModelAndView("/mng/prod/add");
	List <Category> list= categoryService.findAll();
	mav.addObject("cates", list);
	return mav;
}
//新增
@RequestMapping("/add.jhtml")
public String add(Integer cate_id,Product product){
	Category category=categoryService.get(cate_id);
	product.setCategory(category);
	productService.save(product);
	return "redirect:/mng/prod/list.jhtml";
}

//根据Id修改
@RequestMapping("/edit.jhtml")
public String edit(Integer id,Integer cate_id,String  model,String isbn, String name,BigDecimal sale_price,Integer security_num,String summary,String unit,Integer expireTime){
	Product product=productService.get(id);
	product.setCategory(categoryService.get(cate_id));
	product.setIsbn(isbn);
	product.setModel(model);
	product.setName(name);
	product.setSale_price(sale_price);
	product.setSecurity_num(security_num);
	product.setSummary(summary);
	product.setUnit(unit);
	product.setExpireTime(expireTime);
	productService.update(product);
	return "redirect:/mng/prod/list.jhtml";

}

//修改
@RequestMapping("/detail.jhtml")
public ModelAndView detail(Integer id){
	ModelAndView mav=new ModelAndView("/mng/prod/detail");
	Product product=productService.get(id);
	mav.addObject("prod",product);
//	List<Category> list=categoryService.findAll();
	List<Category> list=categoryService.findAll();
	mav.addObject("cates",list);
	return mav;
}
// 删除
	@RequestMapping("/delete.jhtml")
	public String delete(Integer id){
		productService.delete(id);
		return "redirect:/mng/prod/list.jhtml";
	}
//	// 模糊查询
//	@RequestMapping("/search.jhtml")
//	public ModelAndView  Search(String condition)throws Exception{
//		ModelAndView mav=new ModelAndView("mng/prod/list");//?
//		List <Product> list=productService.findByName(condition);
//		mav.addObject("page",list);
//		return mav;
//	}
	
	//删除 list页面
	@RequestMapping(value="/ajaxdelete.jhtml",produces="application/json;charset=UTF-8")
	@ResponseBody
	    public String ajaxdelete(Integer id)throws Exception{
		String rs="";
		try{
			productService.delete(id);
			rs="删除成功！";
		}catch(Exception e){
			rs="该商品已在采购订单中使用！";
		}
		ObjectMapper mapper=new ObjectMapper();
		String json=mapper.writeValueAsString(rs);
		return json;
	}
			
	//使用ajax 加载商品数据  采购订单中使用、
	@RequestMapping(value="/ajaxList.jhtml",produces="application/json;charset=UTF-8")
	@ResponseBody
		public String ajaxList(Integer id)throws Exception{
		List<Product> list=productService.findProducts(id);
		//使用jackSon包来处理json
		ObjectMapper mapper=new ObjectMapper();
		String json=mapper.writeValueAsString(list);
		System.out.println("json:"+json);
		return json;
	}

	//按产品名称查询
	@RequestMapping("/findByName.jhtml")
	public ModelAndView findByName(@RequestParam(name="size",defaultValue="10")int size,
			@RequestParam(name="number",defaultValue="1")int number,String condition){
		ModelAndView mav = new  ModelAndView("mng/prod/list");
		Page<Product> page = productService.findByName(size,number,condition);
		mav.addObject("page",page);
		return mav ;
	}	
	
	//按过期日期查询
	
	@RequestMapping("/findByDate.jhtml")
	public ModelAndView findByDate(@RequestParam(name = "size", defaultValue = "10") int size,
			@RequestParam(name = "number", defaultValue = "1") int number, 
			Date endDate) throws ParseException {
		
	//	DateFormat df = new SimpleDateFormat("y###MMM##d");
		//Date dt = new Date();
//todo	
		//Page<Product> page = productService.findByDate(size,number,df.parse(endDate));
		Page<Product> page = productService.findByDate(size,number,endDate);
		//Page<Product> page = productService.findByDate(size,number,dt);
		ModelAndView mav = new  ModelAndView("mng/prod/list");
		mav.addObject("page", page);
		return mav;
	}
	//// 使用ajax加载商品数据---采购订单中使用
	@RequestMapping(value = "/ajaxDetail.jhtml", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String ajaxDetail(Integer id) throws Exception {
		Product prod = productService.get(id);
		// 使用Jackson包来处理json
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(prod);
		return json;
	}



}
